var global_ajajform_urledit;
(function( $, undefined ) {
/*
	\save true
*/
$.widget("ui.ajajform_urledit",$.ui.ajajform_widget, {
	options: {
		// \type cssclass
		cssclass: "afw-urledit",
		// \type text
		value: "",
		// \type text
		label: "",
		// \type string
		previewSuffix: "-preview1",
		// \type integer
		width: null,
		// \type html
		htmltemplate: '<div class="af-label"></div><div class="af-edit"><input type="text"/><img src="/shared/empty.gif" class="af-preview"/><div class="af-change"><input type="button" value=""/></div><div class="af-error"></div></div><div style="clear: both;"></div>',
//		htmltemplate_disableable: '<div class="af-label"></div><div class="af-disable"></div><div class="af-edit"><input type="text"/><span class="af-html"></span><span class="af-change"></span><div class="af-error"></div></div><div style="clear: both;"></div>',
		// \type function
		validate: null,
		// \type: function
		change: null,
		htmltemplate_dialog: '<div><iframe src="" width="100%" height="95%" frameborder="0" border="0"></iframe></div>'
	},
	i18n: {
		en: {
			change: "Change"
		},
		ru: {
			change: "Сменить"
		}
	},
	_create: function() {
		this.genericCreate();
		this.i18n_texts();
		this.bindChangeButton();
		this.refreshLabel();
		this.refreshInput();
		this.refreshValue();
		this.refreshChange();
		this.refreshDisabled();
	},
	i18n_texts: function() {
		$(".af-change input",this.element).prop("value",this.gettext("change"));
	},
	refreshValue: function() {
		var i=$(".af-edit > input",this.element);
		var img=$(".af-edit img",this.element);
		i.attr("value",this.options.value);
		var arr;
		if (this.options.value) arr=this.options.value.match(/^(\/f\/.*).(png|jpg|gif)$/i);
		if (arr) {
			img.attr("src",arr[1]+this.options.previewSuffix+"."+arr[2]);
		} else {
			img.attr("src","/shared/empty.gif");
		}
	},
	refreshInput: function() {
		var i=$(".af-edit > input",this.element);
		i.addClass("ui-corner-all af-edit");
		i.width(this.options.width);
		i.bind("focus",function(e) { $(this).addClass("ui-state-highlight"); });
		if (this.options.validate) {
			i.bind("blur",this,function(e) {
				$(this).removeClass("ui-state-highlight");
				e.data.validate();
			});
		} else {
			i.bind("blur",function(e) { $(this).removeClass("ui-state-highlight"); });
		}
	},
	bindChangeButton: function() {
		var b=$(".af-change input",this.element);
		var t=this;
		b.button().bind("click",{t:t},function(e) {e.data.t.openFileChooseDialog(e.data.t);});
	},
	openFileChooseDialog: function(t) {
		var d=global_ajajform_file_id_dialog=$(this.options.htmltemplate_dialog).appendTo($("body"));
		$("iframe",d).attr("src","/admin_popup-filebrowser/?target=ajajform_urledit");
		global_ajajform_urledit=t;
		var wx=Math.floor(win_x()*0.90)-100;
		var wy=Math.floor(win_y()*0.90)-100;
		d.dialog({
			modal: true,
			width: wx<830?830:wx,
			height: wy<550?470:wy,
			close: function() { d.dialog("destroy");d.remove(); }
		});
	},
	refreshChange: function() {
/*		var i=$("input",this.element);
		if (!this.options.change) {
			if (this.oldBind) {i.unbind("change",this.oldBind);this.oldBind=null;}
			return;
		}
		if (this.oldBind) i.unbind("change",this.oldBind);
		this.oldBind=this.options.change;
		i.bind("change",{tag:i},this.oldBind);*/
	},
	refreshDisabled: function() {
		$("input",this.element).prop("disabled",this.options.disabled);
		this.genericRefreshDisabled();
	},
	destroy: function() {
	},
	validate: function() {
		return false;
	},
	postvalue: function(r) {
		r[this.options.name]=$("input",this.element).val();
	}
});

})( jQuery );

function ajajform_urledit_set(v)
{
	global_ajajform_urledit._setOption("value",v);
	global_ajajform_urledit=undefined;
	global_ajajform_file_id_dialog.dialog("close");
}

